package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = permissionService.findById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    @RequestMapping("/findAll")
    public Result findAll(Integer id){
        try {
            List<Permission> list = permissionService.findAll(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,list);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            Integer[] ids = permissionService.findPermissionIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,ids);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds){
        try {
            permissionService.edit(role, permissionIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
    }
}
